package com.perkinelmer.housecall.client.activities.capture.impl;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.perkinelmer.housecall.client.activities.capture.Capture;
import com.perkinelmer.housecall.client.activities.capture.CaptureParameter;
import com.perkinelmer.housecall.client.model.Observation;

public class ImageCapture implements Capture {
	
	public static final CaptureId ID = CaptureId.make("ImageCapture");
	
	private static final Image CAMERA_IMAGE = new Image("images/shutter.jpg");
	private static final Image STOCK_IMAGE = new Image("images/tick-bite.jpg");
	
	private final Button takePictureButton;
	private final Button submitButton;
	
	private final Panel viewPort;
	
	private HandlerRegistration takePicHandlerReg = null;
	private HandlerRegistration submitHandlerReg = null;
	
	public ImageCapture() {
		viewPort = new SimplePanel();
		takePictureButton = new Button("Take");
		submitButton = new Button("Submit");
	}
	
	@Override
	public CaptureId getId() {return ID;}

	@Override
	public String getName() {
		return "Image";
	}
	
	@Override
	public void start(Panel panel, final CaptureParameter captureParameter) {
		viewPort.clear();
		viewPort.add(CAMERA_IMAGE);
		
		updateSubmitClickHandler(captureParameter._callback);
		updateTakePicClickHandler(captureParameter._callback);
		
		panel.add(viewPort);
		panel.add(createButtonPanel());
	}

	
	private Panel createButtonPanel() {
		HorizontalPanel panel = new HorizontalPanel();
		panel.add(takePictureButton);
		panel.add(submitButton);
		
		return panel;
	}
	
	private void updateSubmitClickHandler(final CaptureCompleteCallback callback) {
		if(submitHandlerReg != null) {submitHandlerReg.removeHandler();}
		submitHandlerReg = submitButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				Observation observation = createObservation();
				callback.captureComplete(observation);
			}
		});
	}
	
	private void updateTakePicClickHandler(final CaptureCompleteCallback callback) {
		if(takePicHandlerReg != null) {takePicHandlerReg.removeHandler();}
		takePicHandlerReg = takePictureButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				takePicture();
			}
		});
	}
	
	private void takePicture() {
		viewPort.clear();
		viewPort.add(STOCK_IMAGE);
	}
	
	private Observation createObservation() {
		return new ImageObservation();
	}
	
	public static class ImageObservation extends Observation {

		public static final ObservationId ID = ObservationId.make("Image");
		
		@Override
		public ObservationId getId() {return ID;}
	}
}
